<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="名字" prop="voteName">-->
<!--        <el-input-->
<!--          v-model="queryParams.voteName"-->
<!--          placeholder="请输入名字"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['study:vote:add']"
        >新增</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleDelete"
          v-hasPermi="['study:vote:add']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          size="mini"
          @click="handleRank"
          v-hasPermi="['study:vote:Rank']"
        >投票统计</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <h1>{{ votes.title }}</h1>
    <div class="content">
      <div class="show_item" id="ulBreakfast" type="none" v-for="(voteList,index) in showContestants" :key="voteList.id">
        <img class="show_item_img" :src="voteList.image">
        <div class="show_item_checked">
          <input type="checkbox" name="voteList" :value="voteList.id" v-model="checked" id="">
        </div>
        <div class="show_item_show">
<!--          <div>-->
<!--            <p>{{voteList.title}}</p><p>简介:{{voteList.voteName}}</p>-->
<!--          </div>-->
          <div>
            <el-button type="primary" round @click.native="openVote(voteList)">为它投一票</el-button>
          </div>
        </div>
      </div>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="openRank" width="80%" >
        <result :votesId="votes.votesId" v-if="openRank"></result>
    </el-dialog>
    <!-- 添加或修改投票对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="简介" prop="voteName">
          <el-input v-model="form.voteName" placeholder="请输入内容简介" />
        </el-form-item>
        <el-form-item label="添加海报：">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-change="getFile"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVote, getVote, delVote, addVote, updateVote } from "@/api/study/vote";

import result from "@/views/study/vote/result";
import {addInfo, addInfoFile} from "@/api/resource/info";
import votes from "@/views/study/votes";
export default {
  name: "Vote",
  data() {
    return {
      imageUrl:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 投票表格数据
      voteList: [],
      showContestants:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      openRank: false,

      checked: [],

      id:0,
      // 查询参数
      queryParams: {
        id:0,
        pageNum: 1,
        pageSize: 10,
        title: null,
        voteName: null,
        tickets: null,
        image: null
      },
      // 表单参数
      form: {},
      // 表单校验
      votes: {},
      rules: {
      }
    };
  },

  created() {
    console.log("================================zhezhezhe")
    console.log(this.$route.query)
    this.votes = this.$route.query
    this.getList(this.votes.votesId);
  },

  watch: {
    $route: {
      handler() {
        this.votes = this.$route.query
        if (this.votes.votesId != null && this.votes.votesId != "") {
          this.getList(votes.votesId);
        }
      },
      deep: true,
    }
  },

  components: {
    result
  },
  methods: {
    getFile(file){
      this.imageUrl = URL.createObjectURL(file.raw);
      this.file = file.raw;
      console.log(file)
    },

    openVote(item) {
      let _this = this
      this.$confirm('你确定要投他一票吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        _this.addTicket(item)
        _this.$message({
          type: 'success',
          message: '投票成功!'
        });
      }).catch(() => {
        _this.$message({
          type: 'info',
          message: '取消投票'
        });
      });
    },
    addTicket(item){

      let tik = item.tickets;
      let id = item.id;
      tik++;
      this.queryParams.tickets=tik;
      this.queryParams.id=id;
      updateVote(this.queryParams).then(response => {
        console.log(response+"-------------------------")
      });

    },
    /** 查询投票列表 */
    getList() {
      this.loading = true;
      console.log("=======================投标")
      this.queryParams.votesId = parseInt(this.votes.votesId)
      listVote(this.queryParams).then(response => {
        this.voteList = response.rows;
        this.showContestants = this.voteList;
        this.total = response.total;
        this.loading = false;
        console.info(this.voteList);
        console.info(this.showContestants);
      });
    },
    deleteList() {
      console.log("=++++++++++++++++++++++++++")
      for (let i = 0 ; i < this.checked.length ; i++) {
        delVote(this.checked[i])
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        voteName: null,
        tickets: null,
        image: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加投票";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVote(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改投票";
      });
    },
    /** 提交按钮 */
    submitForm() {
      const fd = new FormData();
      fd.append("file",this.file)
      addInfo(fd).then(response=>{
        this.form.image=response.httpPath;
        this.form.votesId = this.votes.votesId
        addVote(this.form).then(res=>{
        this.getList();
        this.open=false;
        });
      })
    },
    /** 删除按钮操作 */
    handleDelete() {
      let _this = this
      this.$modal.confirm('是否确认删除投票编号为的数据项？').then(async function() {
        await _this.deleteList();
        return "wochao"
      }).then(async () => {
        _this.queryParams = {}
        await _this.getList();
        _this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('study/vote/export', {
        ...this.queryParams
      }, `vote_${new Date().getTime()}.xlsx`)
    },
    handleRank() {
      this.openRank = true;
      this.title = "排序";
    }
  }
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.content {
  width: 100%;
  /*height: 500px;*/
  display: flex;
  flex-direction: row;
  justify-content: left;
  flex-wrap: wrap;
}
.show_item {
  position: relative;
  width: 300px;
  height: 400px;
  margin-left: 30px;
  margin-bottom: 30px;
  background-color: pink;
  border-radius: 20px;
  box-shadow: 10px 10px 10px 10px slategray;
  overflow: hidden;
  transition: 1s all;
}
.show_item_img {
  position: relative;
  width: 300px;
  height: 400px;
}
.show_item_show {
  width: 100%;
  position: absolute;
  bottom: 0;
  left: 0;
  display: none;
}
.show_item:hover {
  /*left: -10px;*/
  /*top: -10px;*/
  transform: translate(-10px,-10px);
  display: block;
}
.show_item:hover .show_item_show {
  display: block;
  display: flex;
  justify-content: center;
  margin-bottom: 10px;
}
.show_item_checked {
  position: absolute;
  top: 10px;
  left: 20px;
  z-index: 999;
}
</style>
